package com.lagou.study.pay.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
public class OrderController {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    private static List<String> failList = new ArrayList<>();

    @PostMapping("order")
    public void order(){
        String id = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("ex.pay", "pay", id);
    }

    @PostMapping("pay")
    public String pay(){
        Object o  = rabbitTemplate.receiveAndConvert("q.pay");
        if (o==null){
            return "fail";
        }else{
            return "success";
        }
    }

    @PostMapping("/fail")
    public List<String> fail() {
        String failId = (String) rabbitTemplate.receiveAndConvert("q.pay.fail");
        failList.add(failId);
        return failList;
    }
}
